package com.util;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBSCUtils {
    private static DataSource dataSource;

    static {
        try {
            //载入属性文件
            Properties properties = new Properties();
            properties.load(DBSCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));

            //问问李凯，检查此方法是否需要用静态调用
            BasicDataSourceFactory basicDataSourceFactory = new BasicDataSourceFactory();
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Connection getConn()
    {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {

            if(resultSet != null)
                resultSet.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {

            if(statement != null)
                statement.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {

            if(connection != null)
                connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}